package com.jy.wms.controller;


import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.ManySheetOneSyler;
import com.jy.wms.pojo.WmsProductCategory;
import com.jy.wms.service.WmsProductCategorysService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 订单量汇总报表
 *
 * @author hgl
 * 2018-4-12
 */
@RestController
@RequestMapping("/wmsclassify")
@Api(value = "/wmsclassify", description = "订单量汇总")
public class WmsProductCategoryController extends BaseController<WmsProductCategorysService, WmsProductCategory> {
    @Resource
    private WmsProductCategorysService wmsProductCategorysService;

    /**
     * 分页查询
     *
     * @param entity 实体
     * @return List<WmsProductCategory>
     */
    @PostMapping(value = "/baseHarvestPage")
    @ApiOperation(value = "订单量汇总报表", response = JSONObject.class, notes = "订单量汇总报表分页查询")
    public @ResponseBody
    JSONObject baseHarvestPage(@RequestBody WmsProductCategory entity, HttpSession session) {
        Page<WmsProductCategory> list = new Page<WmsProductCategory>();
        JSONObject json = getJSONObject();
        try {
            // 获取user
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            list = baseService.baseSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    , (int) list.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    @PostMapping(value = "outClassify")
    @ApiOperation(value = "导出订单量汇总报表", response = JSONObject.class, notes = "导出订单量汇总报表")
    public @ResponseBody void outClassify(HttpServletResponse response, @RequestBody WmsProductCategory entity, HttpSession session) throws IOException {
        String fileName = "订单量汇总信息";
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("订单量汇总", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        //WmsProductCategory entity=new WmsProductCategory();
        entity.setCkid(ckid);
        List<WmsProductCategory> list = baseService.exportWmsProductCategory(entity);
        Workbook workbook = ExcelExportUtil.exportExcel(ep, WmsProductCategory.class, list);
        workbook.write(response.getOutputStream());
    }
}